<?php
/**
 *  验证码扩展
 *
 * @copyright	http://www.easymvc.com
 * @author 		光哥  <439884988@qq.com>
 * @version		1.0 - 2012-03-26
 * @package		Extends
 *
 */

class captcha {

function captcha( $params = null )
{
$this->BackgroundImage = $params['BackgroundImage']; //background image
$this->BackgroundColor = $params['BackgroundColor'];
$this->Height = $params['Height']; //image height
$this->Width = $params['Width']; //image width
$this->FontSize = $params['Font_Size']; //text font size
$this->Font = $params['Font']; //text font style
$this->TextMinimumAngle = $params['TextMinimumAngle'];
$this->TextMaximumAngle = $params['TextMaximumAngle'];
$this->TextColor = $params['TextColor'];
$this->TextLength = $params['TextLength'];
$this->Transparency = $params['Transparency'];

$this->generateCode();
//initially, png is used
header("Content-type: image/png");
$this->generateImage($this->Code);

}
//Background Images
function getBackgroundImage()
{
   return $this->BackgroundImage;
}

function setBackgroundImage( $background_image = null )
{
   $this->BackgroundImage = $background_image;
}

//Backgroung Color
function getBackgroundColor()
{
    return $this->BackgroundColor;
}

function setBackgroundColor( $background_color )
{
   $this->BackgroundColor = $background_color;

}

//Image Height
function getHeight()
{
   return $this->Height;
}

function setHeight( $height = null )
{
   $this->Height = $height;
}
//Image Width
function getWidth()
{
   return $this->Width;
}

function setWidth( $width = null )
{
   $this->Width = $width;
}
//Font size
function getFontSize()
{
   return $this->FontSize;
}

function setFontSize( $size = null )
{
   $this->FontSize = $size;
}

//Font
function getFont()
{
   return $this->Font;
}

function setFont( $font = null )
{
   $this->Font = $font;
}

//Text Minimum Angle
function getTextMinimumAngle()
{
   return $this->TextMinimumAngle;
}

function setTextMinimumAngle( $minimum_angle = null )
{
   $this->TextMinimumAngle = $minimum_angle;
}

//Text Maximum Angle
function getTextMaximumAngle()
{
   return $this->TextMaximumAngle;
}

function setTextMaximumAngle( $maximum_angle = null )
{
   $this->TextMaximumAngle = $maximum_angle;
}

//Text Color
function getTextColor()
{
   return $this->TextColor;
}

function setTextColor( $text_color )
{
   $this->TextColor = $text_color;
}

//Text Length
function getTextLength()
{
   return $this->TextLength;
}

function setTextLength( $text_length = null )
{
   $this->TextLength = $text_length;
}

//Transparency
function getTransparency()
{
   return $this->Transparency;
}

function setTransparency( $transparency = null )
{
   $this->Transparency = $transparency;
}

//get Captcha Code
function getCode()
{
return $this->Code;
}

//Generate Captcha
function generateCode()
{
        $length = $this->getTextLength();
        $this->Code = "";
        
        $uppercase  = range('A', 'Z');
        $lowercase  = range('a', 'z');
        $numeric    = range(0, 9);
        
        $CharPool   = array_merge($uppercase, $numeric,$lowercase);
        $PoolLength = count($CharPool) - 1;
        
        for ($i = 0; $i < $length; $i++)
        {
        
        $this->Code .= $CharPool[mt_rand(0, $PoolLength)];
        
        }
        

        return $this->Code;
}


function generateImage($text = null)
{
//$im = imagecreatefrompng( $this->getBackgroundImage() );
$width = $this->getWidth();
$height = $this->getHeight();

 $im       = imagecreate($width, $height);
$tColor = $this->getTextColor();
$txcolor = $this->colorDecode($tColor);
$bcolor = $this->getBackgroundColor();
$bgcolor = $this->colorDecode($bcolor);
$transprency = $this->getTransparency();
$this->im = imagecreate($width,$height);
$bgcolor     = imagecolorallocate($this->im, 255, 255, 255);
//$imgColor = imagecolorallocate($this->im, $bgcolor['red'], $bgcolor['green'], $bgcolor['blue']);
//imagecopymerge($this->im,$im,0,0,0,0,$width,$height,$transprency);
$textcolor = imagecolorallocate($this->im, $txcolor['red'], $txcolor['green'], $txcolor['blue']);
$font = $this->getFont();
$fontsize=$this->getFontSize();
$minAngle = $this->getTextMinimumAngle();
$maxAngle = $this->getTextMaximumAngle();
$length = $this->getTextLength();


$this->signs($this->im, $font);
for($i=0;$i<$length;$i++){
imagettftext(
             $this->im,
             $fontsize,
             rand(-$minAngle,$maxAngle),
             $i*25+10,
             $this->FontSize*1.2,
             $textcolor,
             $font,
             substr($text, $i, 1));
}

imagepng($this->im);
imagedestroy($this->im);
}

function colorDecode( $hex ){

   if(!isset($hex)) return FALSE;

   $decoded['red'] = hexdec(substr($hex, 0 ,2));
   $decoded['green'] = hexdec(substr($hex, 2 ,2));
   $decoded['blue'] = hexdec(substr($hex, 4 ,2));

   return $decoded;

}

function noise (&$image, $runs = 30)
    {
    	
	  $w = imagesx($image);
	  $h = imagesy($image);
  
      for ($n = 0; $n < $runs; $n++)
      {

        for ($i = 1; $i <= $h; $i++)
        {

          $randcolor = imagecolorallocate($image,
                                          mt_rand(0, 255),
                                          mt_rand(0, 255),
                                          mt_rand(0, 255));

          imagesetpixel($image,
                        mt_rand(1, $w),
                        mt_rand(1, $h),
                        $randcolor);

        }

      }  
  
    } //noise
    
    function signs (&$image, $font, $cells = 3)
    {
   	
	  $w = imagesx($image);
	  $h = imagesy($image);

   	  for ($i = 0; $i < $cells; $i++)
   	  {
   	  	   	  	
   	  	$centerX     = mt_rand(1, $w);
   	  	$centerY     = mt_rand(1, $h);
   	  	$amount      = mt_rand(1, 15);
        $stringcolor = imagecolorallocate($image, 175, 175, 175);
   	  	
   	  	for ($n = 0; $n < $amount; $n++)
   	  	{

          $signs = range('A', 'Z');
          $sign  = $signs[mt_rand(0, count($signs) - 1)];

   	  	  imagettftext($image, 25, 
   	  	               mt_rand(-15, 15), 
   	  	               $centerX + mt_rand(-50, 50),
   	  	               $centerY + mt_rand(-50, 50),
   	  	               $stringcolor, $font, $sign);
   	  	
   	  	}
   	  	
   	  }
   	
    } //signs
    
    function blur (&$image, $radius = 3)
    {

	  $radius  = round(max(0, min($radius, 50)) * 2);

	  $w       = imagesx($image);
	  $h       = imagesy($image);
	  
	  $imgBlur = imagecreate($w, $h);

	  for ($i = 0; $i < $radius; $i++)
	  {

		imagecopy     ($imgBlur, $image,   0, 0, 1, 1, $w - 1, $h - 1);
		imagecopymerge($imgBlur, $image,   1, 1, 0, 0, $w,     $h,     50.0000);
		imagecopymerge($imgBlur, $image,   0, 1, 1, 0, $w - 1, $h,     33.3333);
		imagecopymerge($imgBlur, $image,   1, 0, 0, 1, $w,     $h - 1, 25.0000);
		imagecopymerge($imgBlur, $image,   0, 0, 1, 0, $w - 1, $h,     33.3333);
		imagecopymerge($imgBlur, $image,   1, 0, 0, 0, $w,     $h,     25.0000);
		imagecopymerge($imgBlur, $image,   0, 0, 0, 1, $w,     $h - 1, 20.0000);
		imagecopymerge($imgBlur, $image,   0, 1, 0, 0, $w,     $h,     16.6667);
		imagecopymerge($imgBlur, $image,   0, 0, 0, 0, $w,     $h,     50.0000);
		imagecopy     ($image  , $imgBlur, 0, 0, 0, 0, $w,     $h);

	  }
	  
	  imagedestroy($imgBlur);
	  
    } //blur

}
?>